﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Dashboard.Expense.Business;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;

namespace Dashboard.Expense
{
    public partial class FormExpenseTypes : Form
    {
        public int CurrentTypeID { get; set; }

        public FormExpenseTypes()
        {
            InitializeComponent();
        }

        private void QueryApplicationTypes()
        {
            try
            {
                Exp_ExpenseType eet = new Exp_ExpenseType();
                DataTable types = eet.QueryApplicationTypes();

                this.ultraGridApplicationType.DataSource = types;
                this.ultraGridApplicationType.DataBind();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
        }

        private void QueryExpenseData()
        {
            try
            {
                Exp_ExpenseType eet = new Exp_ExpenseType();
                DataTable expenses = eet.QueryExpenseTypes(this.CurrentTypeID);

                this.ultraGridExpense.DataSource = expenses;
                this.ultraGridExpense.DataBind();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
        }

        private void toolStripButtonCreate_Click(object sender, EventArgs e)
        {
            using (FormAddApplicationType addForm = new FormAddApplicationType())
            {
                addForm.ShowDialog(this);

                this.QueryApplicationTypes();
            }
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            this.ultraGridApplicationType.UpdateData();


            List<int> selectedIds = new List<int>();
            foreach (UltraGridRow row in this.ultraGridApplicationType.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value) == true)
                {
                    selectedIds.Add(Convert.ToInt32(row.Cells["Id"].Value));
                }
            }

            if (selectedIds.Count == 0)
            {
                MessageBox.Show(this, "No data selected.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show(this, "Are you sure to delete selected records?", "Confirm",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    this.Cursor = Cursors.WaitCursor;

                    Exp_ExpenseType eet = new Exp_ExpenseType();
                    foreach (int id in selectedIds)
                    {
                        eet.DeleteApplicationTypes(id);
                    }

                    this.CurrentTypeID = 0;
                    this.groupBoxExpense.Text = "Expense types";

                    MessageBox.Show(this, "Delete successfully.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    QueryApplicationTypes();
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void ultraGridMain_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            // 自适应列宽
            e.Layout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.None;
            // 是否允许调整列宽
            e.Layout.Override.AllowColSizing = AllowColSizing.Free;
            // 是否允许移动栏位顺序
            e.Layout.Override.AllowColMoving = AllowColMoving.NotAllowed;

            // 设置Grid的Split窗口个数，建议设置为1--不允许Split
            e.Layout.MaxColScrollRegions = 1;
            e.Layout.MaxRowScrollRegions = 1;

            // 冻结列
            e.Layout.UseFixedHeaders = false;

            // 排序
            e.Layout.Override.HeaderClickAction = Infragistics.Win.UltraWinGrid.HeaderClickAction.SortSingle;

            // 不允许删除
            e.Layout.Override.AllowDelete = DefaultableBoolean.False;

            // 滚动提示
            e.Layout.Bands[0].ScrollTipField = "Code";

            // 隐藏不需要显示的列
            e.Layout.Bands[0].Columns["ID"].Hidden = true;

            for (int i = 0; i < e.Layout.Bands[0].Columns.Count; i++)
            {
                if (e.Layout.Bands[0].Columns[i].Key != "Check")
                {
                    e.Layout.Bands[0].Columns[i].CellActivation = Activation.NoEdit;
                }
                else
                {
                    e.Layout.Bands[0].Columns[i].CellActivation = Activation.AllowEdit;
                }
            }
        }

        private void toolStripButtonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void ultraGridExpense_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            // 自适应列宽
            e.Layout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.None;
            // 是否允许调整列宽
            e.Layout.Override.AllowColSizing = AllowColSizing.Free;
            // 是否允许移动栏位顺序
            e.Layout.Override.AllowColMoving = AllowColMoving.NotAllowed;

            // 设置Grid的Split窗口个数，建议设置为1--不允许Split
            e.Layout.MaxColScrollRegions = 1;
            e.Layout.MaxRowScrollRegions = 1;

            // 冻结列
            e.Layout.UseFixedHeaders = false;

            // 排序
            e.Layout.Override.HeaderClickAction = Infragistics.Win.UltraWinGrid.HeaderClickAction.SortSingle;

            // 不允许删除
            e.Layout.Override.AllowDelete = DefaultableBoolean.False;

            // 滚动提示
            e.Layout.Bands[0].ScrollTipField = "Code";

            // 隐藏不需要显示的列
            e.Layout.Bands[0].Columns["ID"].Hidden = true;
            e.Layout.Bands[0].Columns["TypeID"].Hidden = true;

            for (int i = 0; i < e.Layout.Bands[0].Columns.Count; i++)
            {
                if (e.Layout.Bands[0].Columns[i].Key != "Check")
                {
                    e.Layout.Bands[0].Columns[i].CellActivation = Activation.NoEdit;
                }
                else
                {
                    e.Layout.Bands[0].Columns[i].CellActivation = Activation.AllowEdit;
                }
            }
        }

        private void toolStripButtonNewExpense_Click(object sender, EventArgs e)
        {
            using (FormAddExpense addForm = new FormAddExpense())
            {
                addForm.ShowDialog(this);

                this.QueryExpenseData();
            }
        }

        private void toolStripButtonDeleteExpense_Click(object sender, EventArgs e)
        {
            this.ultraGridExpense.UpdateData();
            
            List<int> selectedIds = new List<int>();
            foreach (UltraGridRow row in this.ultraGridExpense.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value) == true)
                {
                    selectedIds.Add(Convert.ToInt32(row.Cells["ID"].Value));
                }
            }

            if (selectedIds.Count == 0)
            {
                MessageBox.Show(this, "No data selected.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show(this, "Are you sure to delete selected records?", "Confirm",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    this.Cursor = Cursors.WaitCursor;

                    Exp_ExpenseType eet = new Exp_ExpenseType();
                    foreach (int id in selectedIds)
                    {
                        eet.DeleteExpense(id);
                    }

                    MessageBox.Show(this, "Delete successfully.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    QueryExpenseData();
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void FormExpenseTypes_Load(object sender, EventArgs e)
        {
            this.QueryApplicationTypes();
        }

        private void ultraGridApplicationType_ClickCellButton(object sender, CellEventArgs e)
        {
            if (e.Cell.Column.Key == "Expenses")
            {
                this.CurrentTypeID = int.Parse(e.Cell.Row.Cells["ID"].Value.ToString());
                this.groupBoxExpense.Text = "Expense types [Application type : " + e.Cell.Row.Cells["Code"].Value.ToString() + "]";

                this.QueryExpenseData();
            }
        }

    }
}
